#include "../mqserver/exchange.hpp"
#include <gtest/gtest.h>

zhong::ExchangeManager::ptr exchange_manager;

class ExchangeTest : public testing::Environment
{
public:
    virtual void SetUp() override
    {
        exchange_manager = std::make_shared<zhong::ExchangeManager>("./data/meta.db");
    }
    virtual void TearDown() override
    {
        exchange_manager->clear();  // 清理数据库
    }
};
// 插入测试----已成功
TEST(exchange_test, insert_test)
{
    std::unordered_map<std::string, std::string> map = {{"apple", "苹果"}, {"banana", "香蕉"}};
    exchange_manager->declareExchange("exchange1", zhong::ExchangeType::DIRECT, true, false, map);
    exchange_manager->declareExchange("exchange2", zhong::ExchangeType::DIRECT, true, false, map);
    exchange_manager->declareExchange("exchange3", zhong::ExchangeType::DIRECT, true, false, map);
    exchange_manager->declareExchange("exchange4", zhong::ExchangeType::DIRECT, true, false, map);
    ASSERT_EQ(exchange_manager->size(), 4);
}
// 查询测试---已成功
TEST(exchange_test, select_test)
{
    // 注意查询不存在的内容，会出现Segmentation fault (core dumped)

    zhong::Exchange::ptr exchange = exchange_manager->selectExchange("exchange2");
    ASSERT_EQ(exchange->_name, "exchange2");
    ASSERT_EQ(exchange->_durable, true);
    ASSERT_EQ(exchange->_auto_delete, false);
    // ASSERT_EQ(exchange->getArgs(), std::string("banana=香蕉&apple=苹果&"));
}
// 删除测试---已成功
TEST(exchange_test, remove_test)
{
    exchange_manager->deleteExchange("exchange2");
    zhong::Exchange::ptr exchange = exchange_manager->selectExchange("exchange2");
    ASSERT_EQ(exchange.get(), nullptr);
    ASSERT_EQ(exchange_manager->exist("exchange2"), false); // 表示map中的exchange2已经删除
}

int main(int argc, char* argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new ExchangeTest);
    RUN_ALL_TESTS();
}